/******************************************************************************************************
Copyright (C) 2012 Mestrelab Research S.L. All rights reserved.

This file is part of the MNova scripting toolkit.

Authorized users of MNova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.
*****************************************************************************************************/

/*globals Peak, nmr*/
/*jslint plusplus: true, indent: 4*/

if (typeof Peak != 'undefined') {
	if (!Peak.createEnumToStringMap) {
		Peak.createEnumToStringMap = function (aEnum) {
			"use strict";
			var p,
				result = {};

			for (p in aEnum) {
				if (aEnum.hasOwnProperty(p)) {
					result[aEnum[p]] = p;
				}
			}

			return result;
		};
	}

	if (!Peak.peakTypeMap) {
		Peak.peakTypeMap = Peak.createEnumToStringMap(Peak.Types);
	}

	if (!Peak.peakFlagMap) {
		Peak.peakFlagMap = Peak.createEnumToStringMap(Peak.Flags);
	}

	if (!Peak.peakKindMap) {
		Peak.peakKindMap = Peak.createEnumToStringMap(Peak.Kinds);
	}

	if (!Peak.flagsToString) {
		Peak.flagsToString = function (aFlags) {
			"use strict";
			var flag, result = '';

			/*jslint bitwise: true*/
			for (flag in Peak.peakFlagMap) {
				if (Peak.peakFlagMap.hasOwnProperty(flag)) {
					if (aFlags & flag) {
						if (result.length !== 0) {
							result += '+';
						}
						result += Peak.peakFlagMap[flag];
					}
				}
			}
			/*jslint bitwise: false*/
			if (result.length === 0) {
				result = Peak.peakFlagMap[Peak.Flags.None];
			}
			return result;
		};
	}

	if (!Peak.prototype.flagsToString) {
		Peak.prototype.flagsToString = function () {
			"use strict";
			return Peak.flagsToString(this.flags);
		};
	}

	if (!Peak.prototype.compoundLabel) {
		Peak.prototype.compoundLabel = function (aSolvent) {
			"use strict";
			return nmr.peakCompoundLabel(aSolvent, this);
		};
	}

	if (!Peak.prototype.typeToString) {
		Peak.prototype.typeToString = function () {
			"use strict";
			return Peak.peakTypeMap[this.type];
		};
	}

	if (!Peak.prototype.kindToString) {
		Peak.prototype.kindToString = function () {
			"use strict";
			return Peak.peakKindMap[this.kind];
		};
	}
}
